package com.smartstate.dataManagement.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.entity.DeviceCarePlanEntity;
import com.smartstate.dataManagement.entity.Equipment;
import com.smartstate.dataManagement.service.impl.DeviceCarePlanServiceImpl;
import com.smartstate.dataManagement.util.StringUtils;
import com.smartstate.dataManagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@Api(tags = {"设备保养计划控制器"})
@RequestMapping("/deviceCarePlan")
public class DeviceCarePlanController {

    @Autowired
    DeviceCarePlanServiceImpl deviceCarePlanService;


    @PostMapping("/getDeviceByNameOrCode")
    @ApiOperation(value = "根据设备名称和设备编码搜索设备保养数据 , 列表数据分页获取")
    public ResultVO<Page<DeviceCarePlanEntity>> getDeviceByNameOrCode(@RequestBody PageDTO<DeviceCarePlanEntity> pageDTO) {
        return  ResultVO.ok(deviceCarePlanService.queryAllPageByDTO(pageDTO));
    }


    @PostMapping("/addPlan")
    @ApiOperation(value = "新增保养计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceName",value = "设备名称", required = true),
             @ApiImplicitParam(name = "deviceId",value = "设备编码", required = true),
             @ApiImplicitParam(name = "planTime",value = "保养时间77", required = true),
             @ApiImplicitParam(name = "careCycle",value = "保养周期", required = true),
    }
    )
    public ResultVO<Boolean> addPlan(@RequestParam String deviceName, @RequestParam String deviceId, @RequestParam String planTime, @RequestParam String careCycle) {
        return deviceCarePlanService.addPlan(deviceName,deviceId,planTime,careCycle);
    }

    @PostMapping("/getDeviceList")
    @ApiOperation(value = "根据名称获取所有的设备列表")
    public ResultVO<List<Equipment>> getDeviceList(@RequestBody DeviceCarePlanEntity deviceCarePlanEntity) {
        return  ResultVO.ok(deviceCarePlanService.getDeviceList(deviceCarePlanEntity));
    }



    @PostMapping("/updatePlan")
    @ApiOperation(value = "更新保养计划")
    public ResultVO<Boolean> updatePlan(@RequestBody DeviceCarePlanEntity deviceCarePlanEntity) {
        if(StringUtils.isNotBlank(String.valueOf(deviceCarePlanEntity.getId()))){
            return ResultVO.ok(deviceCarePlanService.updateByID(deviceCarePlanEntity));
        }
        return ResultVO.fail("参数缺失，请求失败");
    }



    @PostMapping("/delByID")
    @ApiOperation(value = "根据ID删除单条数据")
    public ResultVO<Boolean> delByID(@RequestBody DeviceCarePlanEntity deviceCarePlanEntity) {
        if(StringUtils.isNotBlank(String.valueOf(deviceCarePlanEntity.getId()))){
            return ResultVO.ok(deviceCarePlanService.deleteById(String.valueOf(deviceCarePlanEntity.getId())));
        }
        return ResultVO.fail("参数缺失，请求失败");
    }


    @PostMapping("/editPlanByID")
    @ApiOperation(value = "根据ID终止设备保养计划")
    public ResultVO<Boolean>  editByID(@RequestBody DeviceCarePlanEntity deviceCarePlanEntity) {
        if(StringUtils.isNotBlank(String.valueOf(deviceCarePlanEntity.getId())) &&
                StringUtils.isNotBlank(deviceCarePlanEntity.getIsAdd())){
            return ResultVO.ok(deviceCarePlanService.editPlanByID(String.valueOf(deviceCarePlanEntity.getId()),deviceCarePlanEntity.getIsAdd()));
        }
        return ResultVO.fail("参数缺失，请求失败");
    }



    @PostMapping("/batchDelByIDS")
    @ApiOperation(value = "根据ID集合批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "ids", required = true),
    }
    )
    public ResultVO<Boolean> batchDelByIDS(@RequestBody List<String> ids) {
        if(ids.size()>0){
            return ResultVO.ok(deviceCarePlanService.batchDelByIds(ids));
        }
        return ResultVO.fail("参数缺失，请求失败");
    }



}
